package com.xcf.controller.business.settlement;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.settlement.receiptinvoice.ReceiptInvoiceService;
import com.xcf.util.Jurisdiction;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;
@Controller
@RequestMapping(value="/receipt")
public class ReceiptInvoiceController extends BaseController{
	String menuUrl = "/receipt/invoice"; //菜单地址(权限用)
	@Resource 
	private  ReceiptInvoiceService receiptinvoiceservice;
	@RequestMapping(value="/invoice")
	public ModelAndView receipt() throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		ModelAndView mv = new ModelAndView();
		String denglu=Jurisdiction.getUsername();//取登陆人的名字
	    pd.put("a", denglu);
	    PageData aa= receiptinvoiceservice.ser(pd);
	    if(aa==null){
		mv.addObject("bus_dept", "");
	    }else
	    {
	    	mv.addObject("bus_dept", aa.getString("DEPARTMENT_NAME"));
	    }
		mv.setViewName("/business/settlement/receiptinvoice/receiptinvoice");
		return mv;
	}
public void quwer(){
	
}
@RequestMapping(value="/list")
@ResponseBody
public ResponseResult<Object> list (int page,int limit) throws Exception{
	
	PageData pd = new PageData();
	pd= this.getPageData();
	PageHelper.startPage(page, limit);
	String denglu=Jurisdiction.getUsername();//取登陆人的名字
    pd.put("a", denglu);
    PageData aa= receiptinvoiceservice.ser(pd);
    if(aa.getString("DEPARTMENT_NAME").contains("商务")){
    pd.put("flag", 2);
    pd.put("bus_sign_status", 1);
    } else if(aa.getString("DEPARTMENT_NAME").contains("财务")){
    	 pd.put("flag", 3);
    	 pd.put("business_sign", 1); 
    }else{
    	 pd.put("flag", 1);
    	 pd.put("bus_sign_status", 1);
    }
	List<PageData>list =receiptinvoiceservice.serch(pd);
	List<PageData>list1= new ArrayList<>();
	for (PageData fis : list) {
		
		fis.put("id", fis.get("id"));
		fis.put("invoice_code", fis.getString("invoice_code"));//发票号
		fis.put("invoice_title", fis.getString("invoice_title"));//发票抬头
		fis.put("invoice_item", fis.getString("invoice_item"));//发票名目
		fis.put("amount", fis.get("amount"));//金额
		fis.put("currency", fis.getString("currency"));//币种
		fis.put("exchange_rate", fis.get("exchange_rate"));//汇率
		fis.put("bus_dept_id", aa.get("DEPARTMENT_ID"));//业务部门id
		String cc=null;
		 if(fis.get("bus_sign_status").toString().equals("1")){
		    cc="已签收";
		 }else{
			 cc="未签收";
		 }
		 fis.put("bus_sign_status", cc);//业务部门签收状态
	    String bb=null;
		if(aa.getString("DEPARTMENT_NAME").contains("商务")){
			bb= "商务部";//部门
			fis.put("business_signer", aa.getString("NAME"));//商务签收人
		}else if(aa.getString("DEPARTMENT_NAME").contains("财务")){
			bb="财务部";//部门
			fis.put("fin_signer", aa.getString("NAME"));//财务签收人
		}else{
			
			bb=aa.getString("DEPARTMENT_NAME");
			
			fis.put("bus_dept_signer", aa.getString("NAME"));// 业务部门签收人
		}
		
		fis.put("bus_dept", bb);//业务部门
		if(fis.get("sign_date")!=null&&fis.get("sign_date")!=""){
			SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
			String str = dateFormater.format(fis.get("sign_date"));
			fis.put("sign_date", str);//签收时间
		}else{
			fis.put("sign_date", fis.get("sign_date"));//签收时间
		}
		fis.put("ticket_holder", fis.getString("ticket_holder"));//开票人
		if(fis.get("ticket_date")!=null &&fis.get("ticket_date")!=""){
			SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
			String str = dateFormater.format(fis.get("ticket_date"));
			fis.put("ticket_date",str );//开票时间
		}else{
			fis.put("ticket_date",fis.get("ticket_date") );//开票时间
		}
		
		 if(fis.get("business_sign").toString().equals("1")){
			 cc="已签收";
		 }else{
			 cc="未签收";
		 }
		 fis.put("business_sign",cc );//商务签收状态
		if(fis.get("business_sign_date")!=null && fis.get("business_sign_date")!=""){
			SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String str = dateFormater.format(fis.get("business_sign_date"));
			fis.put("business_sign_date", str);//商务签收时间
		}else{
			fis.put("business_sign_date", fis.get("business_sign_date"));//商务签收时间
		}
		 if( fis.get("fin_sign").toString().equals("1")){
			 cc="已签收";
		 }else{
			cc="未签收";
		 }
		 fis.put("fin_sign",cc);//财务签收状态
	  if(fis.get("fin_sign_date")!=null && fis.get("fin_sign_date")!=""){
		  SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
			String str = dateFormater.format(fis.get("fin_sign_date"));
			fis.put("fin_sign_date", str);//财务签收时间
	  }else{
		  fis.put("fin_sign_date", fis.get("fin_sign_date"));//财务签收时间
	  }
	  fis.put("ticket_entry_mode", fis.get("ticket_entry_mode"));//来票方式
		list1.add(fis);
	}
	return ResponseResult.success(new PageInfo(list));
}

//业务部门签收
@RequestMapping(value="/add")
@ResponseBody
public String add() throws Exception{
	PageData pd = new PageData();
	String hehe=null;
	
	pd = this.getPageData();
	Date now = new Date(); 
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//
      hehe = dateFormat.format( now ); 
	pd.put("sign_date", hehe);
	receiptinvoiceservice.add(pd);
	return null;	
}
//商务签收
@RequestMapping(value="/udite")
@ResponseBody
public String uddite() throws Exception{
	PageData pd = new PageData();
	String hehe=null;
	pd = this.getPageData();
	Date now = new Date(); 
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//
      hehe = dateFormat.format( now ); 
	pd.put("business_sign_date", hehe);
	receiptinvoiceservice.udite(pd);
	return null;	
}
//财务签收
@RequestMapping(value="/updite")
@ResponseBody
public String updite() throws Exception{
	PageData pd = new PageData();
	String hehe=null;
	pd = this.getPageData();
	Date now = new Date(); 
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//
      hehe = dateFormat.format( now ); 
	pd.put("fin_sign_date", hehe);
	receiptinvoiceservice.updite(pd);
	return null;
	
}
//商务取消签收
@RequestMapping(value="/quxiao")
@ResponseBody
public String quxiao() throws Exception{
	PageData pd = new PageData();
	pd = this.getPageData();
	receiptinvoiceservice.quxiao(pd);
	return null;	
}

//财务取消签收
@RequestMapping(value="/caiwu")
@ResponseBody
public String caiwu() throws Exception{
	PageData pd = new PageData();
	pd = this.getPageData();
	receiptinvoiceservice.caiwu(pd);
	return null;	
}

//业务部门取消签收
@RequestMapping(value="/yewu")
@ResponseBody
public String yewu() throws Exception{
	PageData pd = new PageData();
	pd = this.getPageData();
	receiptinvoiceservice.yewu(pd);
	return null;	
}
}
